package com.chord.algorithms.chapter1;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Gcd {

	/**
	 * 去除vb蠕虫病毒
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("*********************************");
		System.out.println("***VBScript蠕虫病毒html/htm修复工具***");
		System.out.println("***	作者：席文楷  版本1.0	      ***");
		System.out.println("*********************************");
		System.out.println("请输入路径，直接回车将执行全盘修复");
		Scanner sc = new Scanner(System.in);
		String path = sc.nextLine();
		if (path.length() == 0) {
			System.out.println("过程可能较慢，请耐心等待");
			File[] roots = File.listRoots();
			for (File p : roots) {
				System.out.println(p);
				getFileListame("" + p.getAbsolutePath());
			}
		} else
			getFileListame(path);
		System.out.println("执行完毕");
	}

	public static void getFileListame(String strPath) {

		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {

				String filePath = files[i].getAbsolutePath();
				if (filePath.endsWith(".html") || filePath.endsWith(".htm")) {
					dealVirusHtml(filePath);
				}
				if (files[i].isDirectory()) {
					getFileListame(files[i].getAbsolutePath());

				}
			}
		}
	}

	public static void dealVirusHtml(String filename) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("F:\\eclipse\\data3\\algorithms\\log.txt", true));
			br = new BufferedReader(new FileReader(filename));
			String read = null;
			String charset = null;
			boolean flag1 = false;
			while (null != (read = br.readLine())) {
				if (!flag1) {
					if (read.contains("content=\"text/html; charset=")) {
						int beginIndex = read.lastIndexOf("content=\"text/html; charset=") + 28;
						int endIndex = read.indexOf("\"", beginIndex);
						charset = read.substring(beginIndex, endIndex);
						flag1 = true;
					}
				}

				if (read.contains("<SCRIPT Language=VBScript>")) {
					bw.write(filename);
					bw.newLine();
					bw.flush();
					br.close();
					bw.close();
					repairefile(filename, charset);
					break;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bw != null)
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	public static void repairefile(String filename, String charset) throws Exception {
		if (null == charset)
			charset = codeString(filename);
		System.out.println(filename);
		BufferedReader br = null;
		BufferedWriter bw = null;
		File file = new File(filename);
		String dirPath = file.getParent();
		boolean flag1 = false;
		do {
			try {
				bw = new BufferedWriter(
						new OutputStreamWriter(new FileOutputStream(dirPath + "\\dealvirushtml.temp"), charset));
				br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), charset));
				String read = null;
				while (null != (read = br.readLine())) {
					if (read.contains("<SCRIPT Language=VBScript>")) {
						int indexOf = read.indexOf("<SCRIPT Language=VBScript>");
						read = read.substring(0, indexOf);
						bw.write(read);
						bw.newLine();
						bw.flush();
						break;
					}
					bw.write(read);
					bw.newLine();
					bw.flush();
					flag1 = false;
				}
			} catch (UnsupportedEncodingException e) {
				charset = "GBK";
				flag1 = true;
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} while (flag1);
		bw.close();
		br.close();
		boolean delete = file.delete();
		File newFile = new File(dirPath + "\\dealvirushtml.temp");
		boolean renameTo = newFile.renameTo(new File(filename));
		// System.out.println("delete="+delete+", renameTo="+renameTo);
	}

	public static String codeString(String fileName) throws Exception {
		BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
		int p = (bin.read() << 8) + bin.read();
		bin.close();
		String code = null;

		switch (p) {
		case 0xefbb:
			code = "UTF-8";
			break;
		case 0xfffe:
			code = "Unicode";
			break;
		case 0xfeff:
			code = "UTF-16BE";
			break;
		default:
			code = "GBK";
		}

		return code;
	}
}
